import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

/**
 * @author fujianjian
 * @date 2023/8/19 10:57
 **/
public class HostUtils {

    private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private final static DateTimeFormatter shortDateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");

    private final static Random RANDOM = new Random(System.currentTimeMillis());


    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("usage: HostUtils datetime[yyyy-MM-dd HH:mm:ss]");
            return;
        }
        String datetime = args[0];
        LocalDateTime time = LocalDateTime.parse(datetime, formatter);
        prepareStatement(time);
    }

    private static int prepareStatement(LocalDateTime time) throws Exception {

        LocalDateTime secTime = time.plus(1, ChronoUnit.SECONDS);
        String dateStr = time.format(dateFormatter);
        String shortDateStr = time.format(shortDateFormatter);

        int type = 1;
        if (time.getHour() > 12) {
            type += 1;
        }

        Map<String, String> extMap = extData(type);
        extMap.put("lon", String.format(extMap.get("lon"), RANDOM.nextInt(10)));
        extMap.put("lat", String.format(extMap.get("lat"), RANDOM.nextInt(10)));
        extMap.put("distance", String.format(extMap.get("distance"), RANDOM.nextInt(10)));


        String path = String.format("/upload/clockImg/%s/fujianjian_%s_new_%d.jpg", shortDateStr, shortDateStr, type);

        Connection connection = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // connection = DriverManager.getConnection("jdbc:mysql://10.99.2.101:3306/ihr_core?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull",
            //         "ihr_master", "iHR@zhengB!2021");
            // connection = DriverManager.getConnection("jdbc:mysql://10.89.2.119:3306/ihr_core?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull",
            //         "ihr_read_gly", "Ihr@zb$2021");
            connection = DriverManager.getConnection("jdbc:mysql://10.89.2.119:3306/ihr_core?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull",
                    "ihr_master", "iHR@zhengB!2021");
            ps = connection.prepareStatement("INSERT INTO t_att_sign_record (employee_id,sign_time,sign_site,sign_type,is_delete,create_by,create_date,last_update_by,last_update_date,location_id,data_source,longitude,latitude,class_id,image_path,effective_times,attendance_date,image_exception,mobile_imei_exception,location_exception,location_exception_reason,login_id,attendance_status,distance) VALUES " +
                    " (90011645,?,?,'1','N',90011645,?,90011645,?,1604,'4',?,?,'1425367452681175041',?,?,?,'','N','N','','fujianjian','0',?)");

            ps.setObject(1, time);
            ps.setObject(2, extMap.get("site"));
            ps.setObject(3, time);
            ps.setObject(4, secTime);

            ps.setObject(5, extMap.get("lon"));
            ps.setObject(6, extMap.get("lat"));


            ps.setObject(7, path);
            ps.setObject(8, type);
            ps.setObject(9, dateStr);
            ps.setObject(10, extMap.get("distance"));

            System.out.println("sql: "+ ps);
            int count = ps.executeUpdate();
            // int count = 0;
            System.out.println("effective count: " + count);
            return count;
        } finally {
            try {
                if (Objects.nonNull(ps)) {
                    ps.close();
                }
                if (Objects.nonNull(connection)) {
                    connection.close();
                }
            } catch (Exception closeE) {
                closeE.printStackTrace();
            }
        }
    }

    private static Map<String, String> extData(int type) {
        Map<String, String> retMap = new HashMap<>();

        if (type == 1) {
            retMap.put("site", "江西省南昌市青山湖区艾溪湖一路");
            retMap.put("lon", "115.9977406141493%d");
            retMap.put("lat", "28.68295871310763%d");
            retMap.put("distance", "8%d");
        } else if (type == 2) {
            retMap.put("site", "江西省南昌市青山湖区创新一路");
            retMap.put("lon", "115.9975868055555%d");
            retMap.put("lat", "28.68367214626736%d");
            retMap.put("distance", "5%d");
        }

        return retMap;
    }

}
